package com.zhuchaooasystem.ui.login;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zhuchaooasystem.R;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.presenter.login.RegisterContract;
import com.zhuchaooasystem.presenter.login.RegisterPresenter;
import com.zhuchaooasystem.utils.ToastUtil;
import com.zhuchaooasystem.widget.SingleChoiceDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterContract.View {

    @BindView(R.id.act_register_edit_mobile)
    EditText edit_mobile;
    @BindView(R.id.act_register_edit_verify_code)
    EditText edit_verify_code;
    @BindView(R.id.act_register_text_get_code)
    TextView text_get_code;
    @BindView(R.id.act_register_edit_password)
    EditText edit_password;
    @BindView(R.id.act_register_edit_password_again)
    EditText edit_password_again;
    @BindView(R.id.act_register_edit_name)
    EditText edit_name;
    @BindView(R.id.act_register_edit_department)
    EditText edit_department;
    @BindView(R.id.act_register_edit_position)
    EditText edit_position;

    //----获取验证码倒计时
    private CountDownUtil util_time_count_down;
    private final long mills_in_future = 60000; //总时长
    private final long count_down_interval = 1000; //倒计时间隔
    //----
    private List<String> departmentList; //部门列表

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected RegisterPresenter getPresenter() {
        return new RegisterPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {
        //----
        util_time_count_down = new CountDownUtil(mills_in_future, count_down_interval, text_get_code);
        departmentList = mPresenter.getDepartmentList();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDataAndLoadData() {
    }

    @OnClick({R.id.act_register_text_get_code, R.id.act_register_btn_register, R.id.act_register_relative_department})
    public void onClick(View view) {
        switch (view.getId()) {
            //获取短信验证码
            case R.id.act_register_text_get_code:
                if (TextUtils.isEmpty(edit_mobile.getText().toString())) {
                    ToastUtil.showShort(RegisterActivity.this, "请输入手机号");
                    return;
                }
                util_time_count_down.start();
                break;
            //选择技术部门
            case R.id.act_register_relative_department:
                SingleChoiceDialog dialog = new SingleChoiceDialog(RegisterActivity.this);
                dialog.show();
                dialog.setTextTitle("选择部门");
                dialog.setData(departmentList);
                dialog.setOnViewClickListener(new SingleChoiceDialog.OnViewClickListener() {

                    @Override
                    public void onClickOk(SingleChoiceDialog dialog, int position) {
                        edit_department.setText(departmentList.get(position));
                        dialog.dismiss();
                    }

                    @Override
                    public void onClickCancel(SingleChoiceDialog dialog) {
                        dialog.dismiss();
                    }
                });
                break;
            //注册
            case R.id.act_register_btn_register:
                if (TextUtils.isEmpty(edit_verify_code.getText().toString())) {
                    ToastUtil.showShort(RegisterActivity.this, "请输入验证码");
                    return;
                }
                if (TextUtils.isEmpty(edit_password.getText().toString())) {
                    ToastUtil.showShort(RegisterActivity.this, "请输入密码");
                    return;
                }
                if (TextUtils.isEmpty(edit_password_again.getText().toString())) {
                    ToastUtil.showShort(RegisterActivity.this, "请再次输入密码");
                    return;
                }
                if (TextUtils.isEmpty(edit_name.getText().toString())) {
                    ToastUtil.showShort(RegisterActivity.this, "请输入姓名");
                    return;
                }
                if (TextUtils.isEmpty(edit_position.getText().toString())) {
                    ToastUtil.showShort(RegisterActivity.this, "请输入职位");
                    return;
                }
                finish();
                break;
        }
    }


    class CountDownUtil extends CountDownTimer {
        private TextView text_get_code;//点击的按钮

        /**
         * 倒计时
         *
         * @param millisInFuture    总的时间
         * @param countDownInterval 时间间隔
         * @param text_get_code
         */
        public CountDownUtil(long millisInFuture, long countDownInterval, TextView text_get_code) {
            super(millisInFuture, countDownInterval);
            this.text_get_code = text_get_code;
        }


        @Override
        public void onTick(long millisUntilFinished) {
            text_get_code.setClickable(false);
            text_get_code.setText(millisUntilFinished / 1000 + "秒后可重新发送");
        }


        @Override
        public void onFinish() {
            text_get_code.setText("重新获取验证码");
            text_get_code.setClickable(true);//重新获得点击
        }

    }


}
